package com.gzy.literature.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.gzy.literature.entity.Works;
import com.gzy.literature.service.WorksService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;

@RestController
@RequestMapping("works")
public class WorksController extends HttpServlet {
    @Resource
    private WorksService worksService;

    /**
     * 分页查询
     *
     * @param works 筛选条件，作为请求体传递，但这里标记为 required=false，表示可选
     * @param pageNum 页码，从1开始，通过请求参数传递
     * @param pageSize 页面大小，通过请求参数传递
     * @return 查询结果，封装在 ResponseEntity 中
     */
    @GetMapping("/page")
    public ResponseEntity<PageInfo<Works>> queryByPage(@RequestBody(required = false) Works works,
                                                       @RequestParam(defaultValue = "1") int pageNum,
                                                       @RequestParam(defaultValue = "10") int pageSize) {
        if (pageNum <= 0 || pageSize <= 0) {
            return ResponseEntity.badRequest().build(); // 简化返回方式
        }
        try {
            PageInfo<Works> pageInfo = worksService.queryBll(works, pageNum, pageSize);
            return ResponseEntity.ok(pageInfo); // 直接返回 PageInfo 对象
        } catch (Exception e) {
            // 这里可以记录日志，并返回一个更具体的错误信息给客户端
            return ResponseEntity.badRequest().build(); // 简化返回方式
        }
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Works> queryById(@PathVariable("id") String id) {
        if (id == null || id.isEmpty()) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }

        try {
            int workId = Integer.parseInt(id);
            Works work = worksService.queryById(workId);
            if (work == null) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            return ResponseEntity.ok(work);
        } catch (NumberFormatException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
    }
    /**
     * 新增数据
     *
     * @param works 实例对象
     * @return 新增后的实例对象（包含数据库生成的主键等）
     */
    @PostMapping
    public ResponseEntity<Works> insert(@RequestBody Works works) {
        try {
            Works newWork = worksService.insert(works);
            return ResponseEntity.status(HttpStatus.CREATED).body(newWork);
        } catch (Exception e) {
            // 这里可以记录日志，并返回一个更具体的错误信息给客户端
            return ResponseEntity.badRequest().build(); // 简化返回方式
        }
    }
    /**
     * 修改数据
     *
     * @param works 实例对象
     * @return 修改后的实例对象
     */
    @PutMapping
    public ResponseEntity<Works> update(@RequestBody Works works) {
        try {
            Works updatedWork = worksService.update(works);
            return ResponseEntity.ok(updatedWork);
        } catch (Exception e) {
            // 这里可以记录日志，并返回一个更具体的错误信息给客户端
            return ResponseEntity.badRequest().build(); // 简化返回方式
        }
    }
    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("{id}")
    public ResponseEntity<Boolean> deleteById(@PathVariable("id") String id) {
        if (id == null || id.isEmpty()) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(false);
        }
        try {
            int workId = Integer.parseInt(id);
            boolean success = worksService.deleteById(workId);
            if (!success) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(false);
            }
            return ResponseEntity.ok(success);
        } catch (NumberFormatException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(false);
        }
    }
}